# -*- coding: utf-8 -*-
# FileName:     custom_module.py
# time:         22/12/10 010 下午 4:08
# Author:       Zhou Hang
# Description:  I don't want to write
import os

from PySide6.QtCore import Qt, QPoint, QSize
from PySide6.QtGui import QPainter, QColor, QMouseEvent, QIcon
from PySide6.QtWidgets import QMainWindow, QListWidget, QListView, QListWidgetItem


class RoundMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(RoundMainWindow, self).__init__(parent)
        self._tracking = False
        self._startPos = None
        self._endPos = None
        self.padding = 5  # 设置边距
        self.round_width = 20
        # 设置 窗口无边框和背景透明 *必须
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(Qt.FramelessWindowHint)

    def paintEvent(self, event):
        # 圆角
        pat = QPainter(self)
        # pat.setRenderHint(pat.Antialiasing)  # 抗锯齿
        pat.setBrush(QColor(49, 54, 59))  # 仅仅适合qt_material中的dark_pink.xml系列主题
        pat.setPen(Qt.transparent)

        rect = self.rect()
        # 设置矩形距离边框的距离
        rect.setLeft(self.padding)
        rect.setTop(self.padding)
        rect.setWidth(rect.width() - self.padding)
        rect.setHeight(rect.height() - self.padding)
        pat.drawRoundedRect(rect, self.round_width, self.round_width)

    # 无边框情况下也能移动窗口
    def mouseMoveEvent(self, e: QMouseEvent):  # 重写移动事件
        if self._tracking:
            self._endPos = e.pos() - self._startPos
            self.move(self.pos() + self._endPos)

    def mousePressEvent(self, e: QMouseEvent):
        if e.button() == Qt.LeftButton:
            self._startPos = QPoint(e.x(), e.y())
            self._tracking = True

    def mouseReleaseEvent(self, e: QMouseEvent):
        if e.button() == Qt.LeftButton:
            self._tracking = False
            self._startPos = None
            self._endPos = None


class ImageListWidget(QListWidget):
    def __init__(self, parent):
        super(ImageListWidget, self).__init__(parent)
        self.setFlow(QListView.Flow(1))  # 0: left to right,1: top to bottom
        self.setIconSize(QSize(150, 100))

    def add_image_items(self, image_paths=None):
        if image_paths is None:
            image_paths = []
        for img_path in image_paths:
            if os.path.isfile(img_path):
                img_name = os.path.basename(img_path)
                item = QListWidgetItem(QIcon(img_path), img_name.split('.')[0])
                # item.setText(img_name)
                # item.setIcon(QIcon(img_path))
                self.addItem(item)



def main():
    pass


if __name__ == "__main__":
    main()
